﻿/*
NOTE:
Upper Cache Contrain
Easy to handle the load & value
*/
package com.cf.core.Utils{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.EventDispatcher;
	import com.cf.core.events.LoadEvent;
	import flash.net.LocalConnection;  
	import flash.display.Sprite;
	import flash.display.MovieClip;
	// import cacheIMG
	// import cacheXML
	public class cache extends EventDispatcher{
		// _addr child can be cacheIMG, cacheXML or else.
		private var G_addr:Array=new Array;
		
		//
		// Can set global by "cache.global().address" ....etc to use it like a global var;
		private static var G_global:cache;
		public static function global():cache {
			if (G_global==null) {
				G_global= new cache();
			}
			return G_global;
		}//fn
		
		public function address(_s:String,_c:*=null):*{
			if(G_addr[_s]==undefined){
				// debug.msg("cache : ["+_s +"]init type "+ typeof(_c) );
				if(_c!=null){
					try{
						G_addr[_s] = new _c();
					}catch(e){
						G_addr[_s] = _c;	// tools.searchLib('xxx')
					}//try
				}else{
					debug.msg("ERROR cache.address("+_s+",CLASS) :: <CLASS> can't be null when init");
					dispatchEvent(new LoadEvent(LoadEvent.FAIL));
					return Object(undefined);
				}//if
			}//if
			// debug.msg( typeof(G_addr[_s]) );
			return Object(G_addr[_s]);
		}//fn
		
		public function RELEASE(_addr:String=null):void{
			if(_addr!=null){
				// Only delete the selected.
				delete G_addr[_addr];
				// debug.msg("Cache RELEASE ["+ G_addr[_addr] +"] Num="+ G_addr.indexOf(_addr));
			}else{
				// Crash it to release
				/*
				try{
					for(var i in G_addr){
						delete G_addr[i];
					}//for
				}catch(e){}
				//*/
				G_addr = new Array();
				try {
					new LocalConnection().connect('foo');
					new LocalConnection().connect('foo');
				} catch (e:*) {}
				// the GC will perform a full mark/sweep on the second call.
				// debug.msg("ALL Cache have been release");
			}//if
		}//fn

	}//class
}//package